package com.tanhua.manage.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.UserInfoTwoVo;
import com.tanhua.domain.vo.UserInfoVo;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.manage.mapper.StateMapper;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UnfreezeService extends ServiceImpl<StateMapper, UserInfo> {
    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private UserApi userApi;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Value("${tanhua.secret}")
    private String secret;

    private final String TOKEN_KEY = "TOKEN_KEY_";
    /***
     * 接口名称：用户解冻操作
     * @param userId
     * @param reasonsForThawing
     * @return
     */
    public ResponseEntity<Object> Thaw(Integer userId, String reasonsForThawing) {
        UserInfo userInfo = userInfoApi.findById(userId.longValue());
        UserInfoTwoVo vo = new UserInfoTwoVo();
        BeanUtils.copyProperties(userInfo,vo);
            redisTemplate.delete("frozen_key_1_"+userId);
            redisTemplate.delete("frozen_key_2_"+userId);
            redisTemplate.delete("frozen_key_3_"+userId);
        Map<String, String> map = new HashMap<>();
        String message = "";
        map.put(message, "操作成功");
        return ResponseEntity.ok(map);
    }

    /***
     * 接口名称：用户冻结操作
     * @param userId
     * @param freezingTime
     * @param freezingRange
     * @param reasonsForFreezing
     * @param frozenRemarks
     * @return
     */
    public ResponseEntity<Object> freeze(Integer userId, Integer freezingTime, Integer freezingRange, String reasonsForFreezing, String frozenRemarks) {
        User user = userApi.findById(userId.longValue());
        String token = createToken(userId.longValue(), user.getMobile(), secret);

        String value = "理由：" + reasonsForFreezing + "-备注：" + frozenRemarks;
        if(freezingTime==1){
            //冻结3天
            if(freezingRange==1){
//            redisTemplate.expire(key,3L, TimeUnit.DAYS);
                ///String reasonsForFreezing, String frozenRemarks
                redisTemplate.delete(TOKEN_KEY +token);
                redisTemplate.opsForValue().set("frozen_key_1_"+userId, value, Duration.ofMinutes(3));
            }
            if(freezingRange==2){
                redisTemplate.opsForValue().set("frozen_key_2_"+userId, value, Duration.ofDays(3));
            }
            if(freezingRange==3){
                redisTemplate.opsForValue().set("frozen_key_3_"+userId, value,Duration.ofDays(3));
            }
        }else if (freezingTime==2){
            //冻结7天
            if(freezingRange==1){
                redisTemplate.delete(TOKEN_KEY +token);
                redisTemplate.opsForValue().set("frozen_key_1", value, Duration.ofDays(7));
            }
            if(freezingRange==2){
                redisTemplate.opsForValue().set("frozen_key_2_"+userId, value, Duration.ofDays(7));
            }
            if(freezingRange==3){
                redisTemplate.opsForValue().set("frozen_key_3_"+userId, value,Duration.ofDays(7));
            }
        }else if (freezingTime==3){
            //冻结永久 String reasonsForFreezing, String frozenRemarks
            if(freezingRange==1){
                redisTemplate.delete(TOKEN_KEY +token);
                redisTemplate.opsForValue().set("frozen_key_1_"+userId, value);
            }
            if(freezingRange==2){
                redisTemplate.opsForValue().set("frozen_key_2_"+userId, value);
            }
            if(freezingRange==3){
                redisTemplate.opsForValue().set("frozen_key_3_"+userId, value);
            }
        }
        Map<String, String> map = new HashMap<>();
        String message = "";
        map.put(message, "操作成功");
        return ResponseEntity.ok(map);
    }

    public  String createToken(Long userId,String mobile,String secret) {
        // token中存放的数据（可选）
        Map<String,Object> map = new HashMap<>();
        map.put("id",userId);
        map.put("mobile",mobile);
        // 生成token
        String token =
                Jwts.builder().setClaims(map)
                        .signWith(SignatureAlgorithm.HS256, secret).compact();
        return token;
    }
}
